<?php

namespace App\Http\Controllers;

use DB;
use App\Qfenglei;
use App\Carticle;
use App\Circlepic;
use App\Czcomment;
use App\Czcomreply;

use App\Datatables;
use Illuminate\Http\Request;
use Storage;

class QuanziController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * 圈子列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function fenleiList(Request $request)
    {
		if($request->isMethod('post')) {
            return response()->json(Datatables::QfenleiList());
        }
        return view('quanzi.fenlei');
    }

    /**
     * 添加分类
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function fenleiPublish(Request $request)
    {
        if ($request->isMethod('post')) {

            $return = Qfenglei::FenleiPublish($request);
            if ($return['status'] === 1) {
                return redirect('/quanzi/fenlei')->with('status', $return['status'])->with('message', $return['message']);
            } else {
                return back()->with('status', $return['status'])->with('message', $return['message']);
            }
        }

        return view('quanzi.fenleiPublish');
    }


    /**
     * 修改分类
     * @param Request $request
     * @param fenlei $fenlei
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function fenleiModify(Request $request, Qfenglei $Qfenglei)
    {
        if ($request->isMethod('post')) {
            $return = Qfenglei::FenleiModify($request, $Qfenglei);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('quanzi.fenleiModify')->with(compact('Qfenglei'));
    }

    /**
     * 删除分类
     * @param fenlei $fenlei
     * @return array
     * @throws \Exception 
     */
    public function fenleiDel($id, Request $request)
    {
		$fenlei = Qfenglei::where('id', $id)->exists();
        if (!$fenlei) return ['status' => 2, 'message' => '无此分类'];
        return Qfenglei::Del($id, $request);
    }
	
	//刷新分类
	public function shuaxin($id, Request $request){
		$tool = Qfenglei::where('id', $id)->exists();
        if (!$tool) return ['status' => 2, 'message' => '无此分类'];
        return Qfenglei::Shuaxin($id, $request);		
	}

	//圈子列表
    public function zxList(Request $request)
    {
		if($request->isMethod('post')) {
            return response()->json(Datatables::QzsList());
        }
        return view('quanzi.list');
    }
	
	// 添加圈子文章
	public function zxlistPublish(Request $request, Carticle $Carticle)
    {
        if ($request->isMethod('post')) {
            $return = Carticle::zxartPublish($request);
            if ($return['status'] === 1) {
                return redirect('/quanzi/list/'.$Carticle->id)->with('status', $return['status'])->with('message', $return['message']);
            }else{
				return back()->with('status', $return['status'])->with('message', $return['message']);
			}
        }
        return view('quanzi.listPublish')->with(compact('Carticle'));
    }	
	
	//定时发送添加文章
	public function qztimePublish(Request $request){

		//如果是post方法为发帖
        if ($request->isMethod('post')) {
           $return = Carticle::zxartPublish($request);
		   if ($return['status'] === 1) {
                return redirect('/quanzi/timeposing')->with('status', $return['status'])->with('message', $return['message']);
            }
        }

        return view('quanzi.qztposingPublish');		
	}

	//修改圈子文章
	public function zxlistModify($article_id, Request $request)
	{
		$advice = Carticle::find($article_id);
        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }
        if ($request->isMethod('post')) {
            $return = Carticle::zxartcirModify($advice,$request);
			if ($return['status'] === 1) {
                return redirect('/quanzi/list/')->with('status', $return['status'])->with('message', $return['message']);
            }else{
				return back()->with('status', $return['status'])->with('message', $return['message']);
			}
        }
		//封面图
        $cover = DB::table('kl_circle_pic')->select('id', 'pic_path')->where('circle_article_id', $advice->id)->get()->toArray();

        return view('quanzi.listModify')->with(compact('advice','cover'));		
	}
	
	//删除圈子文章
	public function zxlistDel($id, Request $request)
	{
		$gongju = Carticle::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此数据'];
		return response()->json(Carticle::Cdelect($id, $request->message));
	}
	
	public function chexiaodel($id){
        //是否存在该帖子
        if (!DB::table('kl_circle_article')->where('id', $id)->exists()) {
            return ['status' => 2, 'message' => '帖子不存在'];
        }
        //删除帖子
        if (!DB::table('kl_circle_article')->where('id', $id)->delete()) {
            return ['status' => 2, 'message' => '撤销失败'];
        }

        return ['status' => 1, 'message' => '撤销成功'];		
	}	
	
	//圈子图片
	/*public function ztlistpic(Request $request, Carticle $Carticle){
		if($request->isMethod('post')) {
            return response()->json(Datatables::Czpiclist($Carticle->id));
        }
        return view('quanzi.czpiclist')->with(compact('Carticle'));
	}
	// 添加圈子图片
	public function picPublish(Request $request, Carticle $Carticle)
    {
        if ($request->isMethod('post')) {
            $return = Circlepic::Czaddpic($request);
            if ($return['status'] === 1) {
                return redirect('/quanzi/qzpiclist/'.$Carticle->id)->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('quanzi.czpicPublish')->with(compact('Carticle'));
    }
	//修改圈子图片
	public function picModify(Request $request, Circlepic $Circlepic)
	{
        if ($request->isMethod('post')) {
            $return = Circlepic::Czxiugaipic($request, $Circlepic);
			
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('quanzi.czpicModify')->with(compact('Circlepic'));		
	}*/
	//删除圈子图片
	public function picDel($pic_id)
	{
        //删除
        if (!DB::table('kl_circle_pic')->where('id', $pic_id)->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }
        return ['status' => 1, 'message' => '删除成功'];
	}
	
	//圈子评论
	public function czrepay(Request $request,Carticle $Carticle){

		if($request->isMethod('post')) {
            return response()->json(Datatables::Czreplay($Carticle->id));
        }
        return view('quanzi.czrepaly')->with(compact('Carticle'));
	}
	//圈子增加评论
	public function repayPublish(Request $request, Carticle $Carticle){
        if ($request->isMethod('post')) {
            $return = Czcomment::pinglunPublish($request); 
            if ($return['status'] === 1) {
                return redirect('/quanzi/repay/'.$Carticle->id)->with('status', $return['status'])->with('message', $return['message']);
            }else{
				return back()->with('status', $return['status'])->with('message', $return['message']);
			}
        }
        return view('quanzi.repayPublish')->with(compact('Carticle'));		
	}

	//删除圈子评论
	public function czreplyDel($id, Request $request)
	{
		$gongju = Czcomment::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此数据'];
        return Czcomment::plDel($id, $request);
	}
	
	//圈子评论回复
	public function czhuifus($id, Request $request){

		if($request->isMethod('post')) {
            return response()->json(Datatables::Czcomentreplay($id));
        }
        return view('quanzi.czcomentrepaly');
	}

	//删除圈子评论回复
	public function czcomreplyDel($id, Request $request)
	{
		$gongju = Czcomreply::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此数据'];
        return Czcomreply::Del($id, $request);
	}
	
    /**
     * 设置加精
     * @param $advice_id
     * @return array
     */
    public function zxHot($advice_id,Request $request)
    {
        $advice = Carticle::find($advice_id);
        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }
        $advice->is_excellent = Carticle::ISEXC_YES;
		$advice->excellent_time = date("Y-m-d H:i:s",time());
        if (!$advice->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        return ['status' => 1, 'message' => '成功'];
    }

    /**
     * 取消加精
     * @param $advice_id
     * @return array
     */
    public function zxHotoff($advice_id)
    {
        $advice = Carticle::find($advice_id);
        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }
        $advice->is_excellent = Carticle::ISEXC_NO;
        if (!$advice->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        return ['status' => 1, 'message' => '成功'];
    }
	
	
	 /**
     * 设置置顶
     * @param $advice_id
     * @return array
     */
    public function zxTop($advice_id,Request $request)
    {
        $advice = Carticle::find($advice_id);
        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }
        $advice->is_top = Carticle::TOP_YES;
		$advice->title = $request->title;
        if (!$advice->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        return ['status' => 1, 'message' => '成功'];
    }

    /**
     * 取消置顶
     * @param $advice_id
     * @return array
     */
    public function zxTopoff($advice_id)
    {
        $advice = Carticle::find($advice_id);
        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }
        $advice->is_top = Carticle::TOP_NO;
        if (!$advice->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        return ['status' => 1, 'message' => '成功'];
    }
	
	//圈子评论
	public function zxrepay($id, Request $request){

		if($request->isMethod('post')) {
            return response()->json(Datatables::Zxreplay($id));
        }
        return view('zixun.zxrepaly');
	}

	//删除圈子评论
	public function zxreplyDel($id, Request $request)
	{
		$gongju = Zxcomment::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此数据'];
        return Zxcomment::Del($id, $request);
	}
	
	//圈子评论回复
	public function zxhuifus($id, Request $request){

		if($request->isMethod('post')) {
            return response()->json(Datatables::Zxcomentreplay($id));
        }
        return view('zixun.zxcomentrepaly');
	}

	//删除圈子评论回复
	public function zxcomreplyDel($id, Request $request)
	{
		$gongju = Zacomrep::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此数据'];
        return Zacomrep::Del($id, $request);
	}
}
